/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilice.cisteniulic.excel;

import java.util.logging.Level;
import java.util.logging.Logger;
import net.hnilica.cisteniulic.dto.TerminCisOri;
import net.hnilica.cisteniulic.dao.TerminDAO;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.hnilica.cisteniulic.dto.TerminCisOri;
import net.hnilica.cisteniulic.dto.TerminExcel;
import net.hnilica.cisteniulic.dto.TerminExcel;
import net.hnilica.cisteniulic.exception.ParsingExcelMyException;
import net.hnilice.cisteniulic.excel.Parser;

/**
 *
 * @author swosh
 */
public class ExcelEngine {

    private TerminDAO terminDAO = new TerminDAO();
    private Parser parser = new Parser();
    public ExcelEngine() {
    }

    /**
     *
     * @param is
     * @param action
     * @return
     * @throws ParsingExcelMyException
     */
    public synchronized List save(InputStream is) throws ParsingExcelMyException {
        List<String> listInfos = new ArrayList<String>();
        List<TerminExcel> listTerminExcelGood = new ArrayList<TerminExcel>();
        List<TerminExcel> listTerminExcelBad = new ArrayList<TerminExcel>();
        listTerminExcelGood = (List<TerminExcel>) parser.parseOldExcelFile(is);

        List<TerminExcel> terminExcelIterate = new ArrayList<TerminExcel>(listTerminExcelGood);
        listTerminExcelGood.clear();
        for (Iterator<TerminExcel> it = terminExcelIterate.iterator(); it.hasNext();) {

            TerminExcel terminExcel = it.next();
            TerminCisOri terminCisOri2save = terminExcel.getTerminCisOri();
            try {
                terminDAO.saveTermin(terminCisOri2save);
                terminExcel.appendInfo("Byl úspěšně přidán řádek " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + " s hodnotami " + terminExcel.toStringAsTCO() + " ");
                listTerminExcelGood.add(terminExcel);
            } catch (Exception ex) {
                Logger.getLogger(ExcelEngine.class.getName()).log(Level.SEVERE, null, ex);
                terminExcel.appendInfo("Nebyl přidán řádek " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + " ." + ex.getMessage());
                listTerminExcelBad.add(terminExcel);
            }

        }

        listInfos.addAll(getListInfosForTerminCisOri(listTerminExcelGood));
        listInfos.addAll(getListInfosForTerminCisOri(listTerminExcelBad));

        return listInfos;
    }


    public synchronized List update(InputStream is) throws ParsingExcelMyException {
        List<String> listInfos = new ArrayList<String>();
        List<TerminExcel> listTerminExcelGood = new ArrayList<TerminExcel>();
        List<TerminExcel> listTerminExcelBad = new ArrayList<TerminExcel>();
        listTerminExcelGood = (List<TerminExcel>) parser.parseOldExcelFile(is);

        List<TerminExcel> terminExcelIterate = new ArrayList<TerminExcel>(listTerminExcelGood);
        listTerminExcelGood.clear();
        for (Iterator<TerminExcel> it = terminExcelIterate.iterator(); it.hasNext();) {
            TerminExcel terminExcel = it.next();
            TerminCisOri terminCisOriOld = new TerminCisOri(terminExcel.getNazUlice(),
                    terminExcel.getTerCis(), terminExcel.getNazAkce(), terminExcel.getCisOrientacni());

            TerminCisOri terminCisOriNew = new TerminCisOri(terminExcel.getNewNazUlice(),
                    terminExcel.getNewTerCis(), terminExcel.getNewNazAkce(), terminExcel.getNewCisOrientacni());
            try {
                terminDAO.updateTermin(terminCisOriOld, terminCisOriNew);
                terminExcel.appendInfo("Byl úspěšně aktualizován řádek " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + " s hodnotami " + terminCisOriOld.toString()
                        + "Nové hodnoty jsou " + terminCisOriNew.toString());
                listTerminExcelGood.add(terminExcel);
            } catch (Exception ex) {
                Logger.getLogger(ExcelEngine.class.getName()).log(Level.SEVERE, null, ex);
                terminExcel.appendInfo("Nebyl aktualizován řádek " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + ". " + ex.getMessage());
                listTerminExcelBad.add(terminExcel);
            }

        }

        listInfos.addAll(getListInfosForTerminExcel(listTerminExcelGood));
        listInfos.addAll(getListInfosForTerminExcel(listTerminExcelBad));

        return listInfos;
    }

    public synchronized List delete(InputStream is) throws ParsingExcelMyException {
        List<String> listInfos = new ArrayList<String>();
        List<TerminExcel> listTerminExcelGood = new ArrayList<TerminExcel>();
        List<TerminExcel> listTerminExcelBad = new ArrayList<TerminExcel>();
        listTerminExcelGood = (List<TerminExcel>) parser.parseOldExcelFile(is);

        List<TerminExcel> terminExcelIterate = new ArrayList<TerminExcel>(listTerminExcelGood);
        listTerminExcelGood.clear();
        for (Iterator<TerminExcel> it = terminExcelIterate.iterator(); it.hasNext();) {
            TerminExcel terminExcel = it.next();
            TerminCisOri terminCisOri2Delete = new TerminCisOri(terminExcel.getNazUlice(),
                    terminExcel.getTerCis(), terminExcel.getNazAkce(), terminExcel.getCisOrientacni());
            try {
                terminDAO.deleteTermin(terminCisOri2Delete);
                terminExcel.appendInfo("Byl úspěšně smazán termín z řádku " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + " s hodnotami " + terminExcel.toStringAsTCO());
                listTerminExcelGood.add(terminExcel);
            } catch (Exception ex) {
                Logger.getLogger(ExcelEngine.class.getName()).log(Level.SEVERE, null, ex);
                terminExcel.appendInfo("Nebyl smazán termín z řádku " + terminExcel.getRow()
                        + " na listu " + terminExcel.getSheet() + " ." + ex.getMessage());
                listTerminExcelBad.add(terminExcel);
            }
        }

        listInfos.addAll(getListInfosForTerminCisOri(listTerminExcelGood));
        listInfos.addAll(getListInfosForTerminCisOri(listTerminExcelBad));

        return listInfos;
    }


    private List<String> getListInfosForTerminExcel(List<TerminExcel> list) {
        List<String> listString = new ArrayList<String>();
        for (Iterator<TerminExcel> it = list.iterator(); it.hasNext();) {
            TerminExcel terminExcel = it.next();
            String infos = terminExcel.getAllInfo();
            listString.add(infos);
        }
        return listString;
    }

     private List<String> getListInfosForTerminCisOri(List<TerminExcel> list) {
        List<String> listString = new ArrayList<String>();
        for (Iterator<TerminExcel> it = list.iterator(); it.hasNext();) {
            TerminExcel terminExcel = it.next();
            String infos = terminExcel.getAllInfoForOld();
            listString.add(infos);
        }
        return listString;
    }
}


